package com.hb.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hb.commons.ResponseDto;
import com.hb.demo.dto.AdminUserDto;
import com.hb.demo.entity.AdminUser;
import com.hb.demo.service.AdminUserService;
import com.hb.util.ContextUtil;
import com.hb.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 系统用户中心表 前端控制器
 * </p>
 *
 * @author zhaojk
 * @since 2021-08-14
 */
@Api(tags = "用户信息相关操作")
@RestController
public class AdminUserController {

    protected final AdminUserService adminUserService;

    public AdminUserController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @PostMapping("/adminUser/dateTest")
    public ResponseDto dateTest(@RequestBody AdminUserDto userDto) {
        return ResultUtil.success("", userDto.getCreateTime());
    }

    @ApiOperation(value = "测试接口(接口名称)", notes = "测试接口(接口描述)", httpMethod = "POST")
    @PostMapping("/adminUser/test")
    public ResponseDto test(boolean admin) {
        if (admin) {
            boolean exists = adminUserService.exists("admin");
            if (exists) {
                return ResultUtil.SUCCESS;
            }
            AdminUserDto adminUser = new AdminUserDto();
            adminUser.setUname("admin");
            adminUser.setPword("0000");
            adminUser.setRealName("环环小仙女");
            return add(adminUser);
        }
        return ResultUtil.SUCCESS;
    }

    /**
     * 接口参数上添加 @ApiIgnore 注解是为了忽略swagger自动生成的参数描述
     * 可以让前端更直观的看明白参数结构
     */
    @ApiOperation(value = "添加一个用户", notes = "添加一个用户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "用户名", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "pword", value = "密码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "realname", value = "姓名", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "saltSource", value = "盐", dataTypeClass = String.class)
    })
    @PostMapping("/adminUser/add")
    public ResponseDto add(@ApiIgnore @RequestBody AdminUserDto user) {
        // 当前登录的用户
        AdminUser currentUser = ContextUtil.getUserEntity();

        String errorMsg = user.validate();
        if (StrUtil.isNotEmpty(errorMsg)) {
            return ResultUtil.fail(errorMsg);
        }
        if (adminUserService.exists(user.getUname())) {
            return ResultUtil.fail("用户名已被占用");
        }

        // 设置密文密码
        user.buildPword();
        user.buildBaseData(1, currentUser.getUname());

        boolean r = adminUserService.save(user);
        if (r) {
            return ResultUtil.SUCCESS;
        }
        return ResultUtil.FAIL;
    }

    @ApiOperation(value = "删除一个用户", notes = "删除一个用户", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataTypeClass = Long.class, paramType = "path")
    })
    @PostMapping("/adminUser/delete/{id}")
    public ResponseDto delete(@PathVariable Long id) {
        AdminUser user = adminUserService.getById(id);
        if (user == null) {
            return ResultUtil.fail("数据不存在");
        }
        boolean r = adminUserService.removeById(id);
        if (r) {
            return ResultUtil.SUCCESS;
        }
        return ResultUtil.FAIL;
    }

    @ApiOperation(value = "批量删除用户", notes = "批量删除用户", httpMethod = "POST")
    @PostMapping("/adminUser/deleteBatch")
    public ResponseDto deleteBatch(@RequestBody List<Long> ids) {
        boolean r = adminUserService.removeByIds(ids);
        if (r) {
            return ResultUtil.SUCCESS;
        }
        return ResultUtil.FAIL;
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "用户名", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "realname", value = "姓名", required = true, dataTypeClass = String.class)
    })
    @PostMapping("/adminUser/update")
    public ResponseDto update(@RequestBody AdminUserDto user) {
        Long id = user.getId();
        if (id == null) {
            return ResultUtil.fail("缺少必要参数");
        }

        // 更改用户信息不会改更密码(更改密码调用单独接口)
        user.setPword(null);

        AdminUser orgUser = adminUserService.getById(id);
        BeanUtil.copyProperties(user, orgUser, CopyOptions.create().ignoreNullValue());
        boolean r = adminUserService.updateById(orgUser);
        if (r) {
            return ResultUtil.SUCCESS;
        }
        return ResultUtil.FAIL;
    }

    @ApiOperation(value = "查询用户", notes = "查询用户", httpMethod = "POST")
    @PostMapping("/adminUser/query")
    public ResponseDto query(@RequestBody AdminUser user) {
        QueryWrapper<AdminUser> wrapper = Wrappers.query();
        wrapper.like(StrUtil.isNotEmpty(user.getUname()), "uname", user.getUname())
                .or()
                .like(StrUtil.isNotEmpty(user.getRealName()), "realname", user.getRealName());
        List<AdminUser> list = adminUserService.list(wrapper);
        return ResultUtil.msg(true, list);
    }
}
